package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.CmsSpecialCommentService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.CmsSpecialCommentMapper;
import org.darkforest.dao.mapper.CmsSpecialCommentUpMapper;
import org.darkforest.model.Member;
import org.darkforest.model.RestEntity;
import org.darkforest.model.CmsSpecialComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "specialComment")
@Api(value = "specialComment", description = "专题评论留言",produces= MediaType.APPLICATION_JSON_VALUE)
public class SpecialCommentController {
    @Autowired
    private CmsSpecialCommentMapper specialCommentMapper;
    @Autowired
    private CmsSpecialCommentUpMapper specialCommentUpMapper;

    @Autowired
    private CmsSpecialCommentService specialCommentService;

    @AuthPassport
    @RequestMapping(value = "addComment", method = RequestMethod.POST)
    @ApiOperation(value = "新增评论留言", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity addComment(@CurrentMember Member member, @RequestBody CmsSpecialComment specialComment) {

        try {

            specialComment.setCommentMemberId(member.getMemberId());
            specialComment.setCommentTime(DateUtil.unixTimeStampInt().intValue());
            specialComment.setCommentUp(0);
            specialComment.setCommentStatus((byte) 1);

            specialCommentMapper.insertSelective(specialComment);

            return RestEntity.success(specialComment);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @AuthPassport
    @RequestMapping(value = "thumbsUp", method = RequestMethod.POST)
    @ApiOperation(value = "点赞", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity thumbsUp(@CurrentMember Member member, @RequestBody CmsSpecialComment specialComment) {

        try {
            Integer memberId = member.getMemberId();

            specialCommentService.addThumbUp(specialComment, memberId);

            return RestEntity.success();
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @AuthPassport
    @RequestMapping(value = "cancelThumbsUp", method = RequestMethod.POST)
    @ApiOperation(value = "取消点赞", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity cancelThumbsUp(@CurrentMember Member member, @RequestBody CmsSpecialComment specialComment) {

        try {
            Integer memberId = member.getMemberId();

            specialCommentService.cancelThumbUp(specialComment, memberId);

            return RestEntity.success();
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
